package com.mszlu.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mszlu.news.dao.mapper.ArticleMapper;
import com.mszlu.news.dao.mapper.GoodMapper;
import com.mszlu.news.dao.mapper.SysUserMapper;
import com.mszlu.news.dao.pojo.Article;
import com.mszlu.news.dao.pojo.Good;
import com.mszlu.news.dao.pojo.SysUser;
import com.mszlu.news.service.GoodService;
import com.mszlu.news.vo.Result;
import com.mszlu.news.vo.params.CollectParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodServiceImpl implements GoodService {
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Result listGoodByUserId(Long userId) {
        List<Good> goodList=goodMapper.listGoodByUserId(userId);
        return Result.success(goodList);
    }

    @Override
    public Result addGood(CollectParam collectParam) {
        Good good=new Good();
        good.setArticleId(collectParam.getArticle_id());
        good.setUserId(collectParam.getUser_id());
        goodMapper.insert(good);
        Article article=articleMapper.selectById(collectParam.getArticle_id());
        int GoodCounts=article.getGoodCounts();
        article.setId(collectParam.getArticle_id());
        article.setGoodCounts(GoodCounts+1);//设置点赞数+1
        articleMapper.updateById(article);
        return Result.success("点赞成功");
    }

    @Override
    public Result deleteGood(CollectParam collectParam) {
        Map<String,Object> columnMap = new HashMap<>();
        columnMap.put("article_id",collectParam.getArticle_id());
        columnMap.put("user_id",collectParam.getUser_id());
        goodMapper.deleteByMap(columnMap);
        Article article=articleMapper.selectById(collectParam.getArticle_id());
        int GoodCounts=article.getGoodCounts();
        article.setId(collectParam.getArticle_id());
        article.setGoodCounts(GoodCounts-1);//设置点赞数+1
        articleMapper.updateById(article);
        return Result.success("取消点赞成功");
    }

    @Override
    public Result GoodCounts(Long articleId) {
        QueryWrapper<Good> query = new QueryWrapper<>();
        query.eq("article_id",articleId);
        int GoodCounts=goodMapper.selectCount(query);
        Article article=new Article();
        article.setId(articleId);
        article.setGoodCounts(GoodCounts);
        articleMapper.updateById(article);
        return Result.success(GoodCounts);
    }

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Result GoodCounts1(Long userId) {
        Integer GoodCounts=goodMapper.GoodCountsByUserId(userId);
        SysUser sysUser=new SysUser();
        sysUser.setId(userId);
        sysUser.setGoodCounts(GoodCounts);
        sysUserMapper.updateById(sysUser);
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("goodCounts",GoodCounts);
        return Result.success(hashMap);
    }
}
